package vip.xiaonuo.core.handler;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import vip.xiaonuo.auth.core.util.StpClientUtil;
import vip.xiaonuo.sys.api.SysSecApi;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
public class SecDataQueryHandler implements DataPermissionHandler {
    @Override
    public Expression getSqlSegment(Expression where, String mappedStatementId) {
        log.info("=========================== start MyDataPermissionHandler");
        log.info(mappedStatementId);
        if(mappedStatementId.contains("DevFile")) {
            if (!StpUtil.isLogin()) {
                return where;
            }
            log.info("id",StpUtil.getLoginIdAsString());

            String secLevel = (String) StpUtil.getSession().get("userSecLevel");
            if (secLevel!=null){
                MinorThanEquals exp = new MinorThanEquals();
                exp.setLeftExpression(new Column("sec_level"));
                exp.setRightExpression(new LongValue(Long.valueOf(secLevel)));
                return new AndExpression(where,  exp);
            }
        }
        return where;
    }


}
